package univ.uchiha.esiag.gon.topic;

import java.util.StringTokenizer;
import java.util.Vector;

import univ.uchiha.esiag.gon.accesser.DataWriter;
import univ.uchiha.esiag.gon.data.DataObject;
import univ.uchiha.esiag.gon.domaine.local.impl.LocalDomaineManagerImpl;
import univ.uchiha.esiag.gon.operation.Operation;

public class Topic {
	private Vector<Topic> topics;
	private DataObject data;
	private String simpleName;
	private String name;
	private DataWriter dataWriter;
	private Topic(String name, String simpleName) {
		this.name = name + "." + simpleName;
		this.simpleName = simpleName;
		topics = new Vector<Topic>();
	}
	public void setDataWriter(DataWriter dataWriter) {
		this.dataWriter = dataWriter;
	}
	public DataWriter getDataWriter() {
		return dataWriter;
	}
	public Topic(String simpleName) {
		this.name = simpleName;
		this.simpleName = simpleName;
		topics = new Vector<Topic>();
	}
	public void bootstrappe(){
		dataWriter.write(new Operation(Operation.NOTIFY, false, Operation.TOPIC, new DataObject(getName(), null)));
	}
	public String getName() {
		return name;
	}
	public void createTopic(Operation operation){
		createTopic(operation.getTopicNameTarget(), 0);
		if(operation.isReplicated())
			dataWriter.write(operation);
	}
	public void deleteTopic(Operation operation){
		if(operation.isReplicated())
			dataWriter.write(operation);
	}
	public void createCache(Operation operation){
		data = operation.getData();
		if(operation.isReplicated())
			dataWriter.write(operation);
	}
	public void updateCache(Operation operation){
		this.data = operation.getData();
		if(operation.isReplicated())
			dataWriter.write(operation);
	}
	public void deleteCache(Operation operation){
		data = null;
		if(operation.isReplicated())
			dataWriter.write(operation);
	}
	public DataObject getData() {
		return data;
	}
	public void notifyAuthers(){
		if(topics.size() == 0){
			dataWriter.write(new Operation(Operation.CREATE, false, Operation.TOPIC, new DataObject(getName(), null)));
		}
		else{
			for(int i = 0;i<topics.size();i++){
				topics.get(i).notifyAuthers();
			}
		}
	}
	private void createTopic(String tName, int number){
		String simpleName1 = getTopicAtNumber(tName, number);
		if(simpleName1 == null)
			return;
		if(simpleName1.equals(this.simpleName)){
			number++;
			String simpleName2 = getTopicAtNumber(tName, number);
			if(simpleName2 == null)
				return;
			for(int i = 0;i<topics.size();i++){
				if(topics.get(i).getSimpleName().equals(simpleName2)){
					topics.get(i).createTopic(tName, number);
					return;
				}
			}
			createLocalTopic(tName, number).createTopic(tName, number);
		}
	}
	private Topic createLocalTopic(String tName, int number){
		String simpleName = getTopicAtNumber(tName, number);
		Topic topic = new Topic(name, simpleName);
		LocalDomaineManagerImpl.getInstance().addTopic(topic);
		topics.add(topic);
		return topic;
	}
	public String getSimpleName(){
		return simpleName;
	}
	private String getTopicAtNumber(String tName, int number){
		StringTokenizer st = new StringTokenizer(tName, ".");
		int i = 0;
		while(st.hasMoreTokens() != false){
			String name = st.nextToken();
			if(i == number){
				return name;
			}
			i++;
		}
		return null;
	}
	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append(simpleName);
		sb.append(topics.toString());
		return sb.toString();
	}
}
